package org.jdcenter.server.xb.metadata;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
 * @author miao
 * @since 2008-12-02
 *
 */
import org.jdcenter.server.xb.visitors.AbstractVisitor;

public class PluginsMateData implements MateDataSet{
	
	private Set<MateDataList> mateDatas;
	private Map<Thread,Set<Class>> threadVar;
	
	public PluginsMateData(){
		mateDatas = new HashSet<MateDataList>();
		threadVar = new HashMap<Thread,Set<Class>>();
	}
	
	public void accept(AbstractVisitor dm){
		dm.visitMateData(this);
		if(mateDatas != null && mateDatas.size() > 0){
			for(MateData mateData:mateDatas){
				mateData.accept(dm);
			}			
		}
	}
	
	public void addMateData(MateDataList mateData){
		this.mateDatas.add(mateData);
	}
	
	public void addDataSet(Set<MateDataList> dataSet){
		this.mateDatas.addAll(dataSet);
	}
	
	public void setChildMateData(MateDataList dataList,Class errorClass) throws MateDataException{
		Set<Class> clazzs = threadVar.get(Thread.currentThread());
		if(clazzs == null){
			clazzs = new HashSet<Class>();
			threadVar.put(Thread.currentThread(), clazzs);
		}
		clazzs.add(errorClass);
		for(MateDataList mateData:mateDatas){
			if(!clazzs.contains(mateData.getClass())){
				mateData.setMateData(dataList);
				threadVar.remove(Thread.currentThread());
				return;
			}
		}
		threadVar.remove(Thread.currentThread());
		throw new MateDataException(dataList.getClass().getName()
				+" is not exist in DataSet");
	}

}
